package com.library.controller;

import com.library.service.BookInfoService;
import com.library.service.LendListService;
import com.library.service.ReaderInfoService;
import com.github.pagehelper.PageInfo;
import com.library.po.BookInfo;
import com.library.po.LendList;
import com.library.po.ReaderInfo;
import com.library.utils.DataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.library.utils.Constants.FAIL_MSG;

@Controller
public class LendListController {
    @Autowired
    private LendListService lendListService;
    @Autowired
    private ReaderInfoService readerService;

    @Autowired
    private BookInfoService bookInfoService;

    @GetMapping("/lendListIndex")
    public String lendListIndex(){
        return "lend/lendListIndex";
    }
    @GetMapping("/lendListIndex2")
    public String lendListIndex2(){
        return "lend/lendListIndex2";
    }

    @ResponseBody
    @RequestMapping("/lendListAll")
    public DataInfo lendListAll(Integer type, String readerNumber, String name, Integer status,
                                @RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "15")Integer limit){
        LendList info=new LendList();
        info.setBackType(type);
        ReaderInfo reader=new ReaderInfo();
        reader.setReaderNumber(readerNumber);
        info.setReaderInfo(reader);
        BookInfo book=new BookInfo();
        book.setName(name);
        book.setStatus(status);
        info.setBookInfo(book);
        PageInfo pageInfo=lendListService.queryLendListAll(info,page,limit);
        return DataInfo.ok("ok",pageInfo.getTotal(),pageInfo.getList());
    }

    @GetMapping("/addLendList")
    public String addLendList(){
        return "lend/addLendList";
    }

    @GetMapping("/excBackBook")
    public String excBackBook(HttpServletRequest request, Model model){
        String id=request.getParameter("id");
        String  bId=request.getParameter("bookId");
        model.addAttribute("id",id);
        model.addAttribute("bid",bId);
        return "lend/excBackBook";
    }

    @RequestMapping("/queryLookBookList")
    public String queryLookBookList(String flag,Integer id,Model model){
        List<LendList> list=null;
        if(flag.equals("book")){
            list=lendListService.queryLookBookList(null,id);
        }else{
            list=lendListService.queryLookBookList(id,null);
        }
        model.addAttribute("info",list);
        return "lend/lookBookList";
    }

    @RequestMapping("/queryLookBookList2")
    public String queryLookBookList(HttpServletRequest request,Model model){
        ReaderInfo readerInfo = (ReaderInfo) request.getSession().getAttribute("user");
        List<LendList> list = lendListService.queryLookBookList(readerInfo.getId(),null);
        model.addAttribute("info",list);
        return "lend/lookBookList";
    }

    @ResponseBody
    @RequestMapping("/lendListAll2")
    public DataInfo lendListAll2(Integer type, String readerNumber, String name, Integer status,
                                 @RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "15")Integer limit,HttpServletRequest request){

        LendList info=new LendList();
        info.setBackType(type);
        ReaderInfo reader=new ReaderInfo();
        reader.setReaderNumber(readerNumber);
        ReaderInfo readerInfo = (ReaderInfo) request.getSession().getAttribute("user");
        Integer id = readerInfo.getId();
        reader.setId(id);
        info.setReaderInfo(reader);
        BookInfo book=new BookInfo();
        book.setName(name);
        book.setStatus(status);
        info.setBookInfo(book);
        PageInfo pageInfo=lendListService.queryLendListAll2(info,page,limit);
        return DataInfo.ok("ok",pageInfo.getTotal(),pageInfo.getList());
    }
}
